#!/usr/bin/env python
# -*- coding: utf-8 -*-

import sys
import urllib2
import re
import time
import socket
import date

#canking_path = "/Users/yanfei/Work/pycode/get_canking.log"
canking_path = "./get_canking.log"
canking_url = "http://www.canking.com/page/"
page_reg = r'<div class="postCon">(.*?)</div>'


class Canking():

    def __init__(self):
        self.html_data = []

    def get_html(self, start_page=1, end_page=3):
        for i in range(start_page, end_page + 1):
            print("----正在采集%d页的数据----" % i)
            rt = urllib2.Request(canking_url + str(i))
            try:
                my_data = urllib2.urlopen(rt).read().replace("\n", "")
                self.html_data.append(my_data)
                time.sleep(2)
                socket.setdefaulttimeout(15)
            except urllib2.URLError, e:
                if hasattr(e, 'reason'):
                    print u"连接失败", e.reason
        return str(self.html_data)


class GetData():

    def __init__(self):
        self.html = Canking().get_html()

    def get_sub_Content(self, item):
        all_reg = r'.*?href="(.*?)" title="(.*?)">.*?</a>.*?"nofollow">(.*?)</a-->.*?(\d{2}-\d{2}-\d{2}).*?&nbsp;.*?(\d{1,5}).*?'
        info_match = re.match(all_reg, item)
        return info_match.groups()

    def get_Content(self):
        final_list = []
        pattern = re.compile(page_reg)
        data = re.findall(pattern, self.html)
        for i in range(0, len(data)):
            r = self.get_sub_Content(data[i])
            final_list.append(str(r).decode("unicode_escape").encode("utf-8"))
        return final_list


if __name__ == "__main__":
    with open(canking_path, 'w') as file:
        final_data = GetData().get_Content()
        file.write(str(final_data))

        for j in final_data:
            print "================"
            print j

